<template>
    <el-card class="account-container">
        <h1>添加商品</h1>
        <div style="line-height: 100px">

            <el-form :model="formData" label-width="120px">
                <el-form-item label="商品名称" >
                    <el-input  v-model="formData.name" />
                </el-form-item>
                <el-form-item label="商品价格">
                    <el-input  v-model="formData.price" />
                </el-form-item>
                <el-form-item label="商品库存">
                    <el-input  v-model="formData.inventory" />
                </el-form-item>
                <el-form-item label="商品类别" >
                    <el-select  v-model="typeValue" value-key="dishTypeId" placeholder="选择商品类别" @change="changeType">
                        <el-option
                                v-for="item in arrType"
                                :key="item.dishTypeId"
                                :label="item.typeName"
                                :value="item"
                        />
                    </el-select>
                    <el-select  v-model="categoryValue" value-key="id" placeholder="选择商品类别" @change="changeCategory">
                        <el-option
                                v-for="item in arrCategory"
                                :key="item.id"
                                :label="item.name"
                                :value="item"
                        />
                    </el-select>
                    </el-form-item>
                <el-form-item label="排名等级" prop="level">
                    <el-select v-model="formData.level" placeholder="选择商品类型">
                        <el-option label="置顶" value="1" />
                        <el-option label="靠前" value="2" />
                        <el-option label="居中" value="3" />
                        <el-option label="靠后" value="4" />
                    </el-select>
                </el-form-item>
                <el-form-item label="商品描述">
                    <el-input v-model="formData.description" type="textarea" :rows="6" />
                </el-form-item>
                <el-form-item label="商品图片">
                    <el-upload
                            class="avatar-uploader"
                            action="api/merchant/upload"
                            name="file"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="formData.image" :src="formData.image" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-card>
</template>

<script  setup>
    import { reactive } from 'vue'
    import axios from "axios";
    import {useStore} from 'vuex'
    import {useRouter} from 'vue-router'
    import { ref } from 'vue'
    import { ElMessage } from 'element-plus'
    import { Plus } from '@element-plus/icons-vue'

    // do not use same name with ref
    const store=useStore()
    const router=useRouter()
    let arrType=ref();
    let arrCategory=ref();
    let categoryValue=ref();
    let typeValue=ref();
    const formData = reactive({
        name:'',
        price:'',
        level:'3',
        categoryId:'',
        description: '',
        image:'',
        inventory:''

    })
    axios.get("/api/merchant/getDishType",{
    }).then(res=>{
        arrType.value=res.data.data;
     //   console.log(res)
    })
    const changeType=(val)=>{
        console.log(val)
        axios.get("/api/merchant/getCategory", {
            params:{
                typeId:val.dishTypeId
            }
        }).then(res=>{
            arrCategory.value=res.data.data
        })
    }
    const changeCategory=(val)=>{
        formData.categoryId=val.id
        console.log(formData)
    }
    const handleAvatarSuccess=(res)=>{
        formData.image=res.data
    }
    const onSubmit=()=>{

        axios.post("/api/merchant/addDish",formData).then(res=>{
            if(res.data.state==0){
                ElMessage.success("添加成功")
                router.push("/good")
            }else{
                ElMessage.error(res.data.message)
            }
        })
    }
</script>

<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>